package com.qianfeng.tiantian_banjia.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import android.content.Context;

public class UploadFileUtil {

	/**
	 * 鎻愪氦鍙傛暟閲屾湁鏂囦欢鐨勬暟鎹�
	 * 
	 * @param url
	 *            鏈嶅姟鍣ㄥ湴鍧�
	 * @param param
	 *            鍙傛暟
	 * @return 鏈嶅姟鍣ㄨ繑鍥炵粨鏋渏son涓�
	 * @throws Exception
	 */
	public static String uploadSubmit(Context context, String url,
			Map<String, String> param) throws Exception {

		HttpPost post = new HttpPost(url);

		/**
		 * 娉ㄦ剰杩欓噷濡傛灉鍙敤new MultipartEntity()鏃犲弬鐨勬瀯閫犳柟娉曪紝鍒欎紶杩囧幓鐨勪腑鏂囨槸涔辩爜
		 */
		MultipartEntity entity = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE, null,
				Charset.forName("UTF-8"));

		if (param != null && !param.isEmpty()) {
			for (Map.Entry<String, String> entry : param.entrySet()) {
				entity.addPart(entry.getKey(), new StringBody(entry.getValue(),
						Charset.forName("UTF-8")));
			}
		}
		// 娣诲姞鏂囦欢鍙傛暟
		// if (file != null && file.exists()) {
		// entity.addPart("file", new FileBody(file));
		// }
		post.setEntity(entity);
		HttpResponse response = HttpClient.getHttpClient(context).execute(post);
		int stateCode = response.getStatusLine().getStatusCode();
		StringBuffer sb = new StringBuffer();
		if (stateCode == HttpStatus.SC_OK) {
			HttpEntity result = response.getEntity();
			if (result != null) {
				InputStream is = result.getContent();
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				String tempLine;
				while ((tempLine = br.readLine()) != null) {
					sb.append(tempLine);
				}
			}
		}
		post.abort();
		return sb.toString();
	}

	public static String uploadImage(Context context, String url,
			Map<String, String> param, File file) throws Exception {

		try {
			HttpPost post = new HttpPost(url);

			/**
			 * 娉ㄦ剰杩欓噷濡傛灉鍙敤new MultipartEntity()鏃犲弬鐨勬瀯閫犳柟娉曪紝鍒欎紶杩囧幓鐨勪腑鏂囨槸涔辩爜
			 */
			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE, null,
					Charset.forName("UTF-8"));
			if (param != null && !param.isEmpty()) {
				for (Map.Entry<String, String> entry : param.entrySet()) {
					entity.addPart(
							entry.getKey(),
							new StringBody(entry.getValue(), Charset
									.forName("UTF-8")));
				}
			}
			// 娣诲姞鏂囦欢鍙傛暟
			if (file != null && file.exists()) {
				entity.addPart("file", new FileBody(file));
			}
			post.setEntity(entity);
			HttpResponse response = HttpClient.getHttpClient(context).execute(
					post);
			int stateCode = response.getStatusLine().getStatusCode();
			StringBuffer sb = new StringBuffer();
			if (stateCode == HttpStatus.SC_OK) {
				HttpEntity result = response.getEntity();
				if (result != null) {
					InputStream is = result.getContent();
					BufferedReader br = new BufferedReader(
							new InputStreamReader(is));
					String tempLine;
					while ((tempLine = br.readLine()) != null) {
						sb.append(tempLine);
					}
				}
			}
			post.abort();
			return sb.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
